//
//  DHTabBarViewController.m
//  DHKuaiYi
//
//  Created by xiaohuihui on 2016/11/13.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHTabBarViewController.h"
#import "DHNavViewController.h"
#import <MMDrawerController.h>
#import <UIViewController+MMDrawerController.h>
#import "DHBaseViewController.h"

#define ClassKey @"className"
#define nameKey  @"name"
#define imageKey @"imageNme"

@interface DHTabBarViewController ()

@property (nonatomic, strong) UIViewController *vc;
//记录按钮
@property (nonatomic, weak) UIButton *LeftItem;

@end

@implementation DHTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addViewControllers];
}

/**
 添加控制器
 */
- (void)addViewControllers {
    
    //
    NSArray *childItemsArray = @[
                                 @{ClassKey :
                                       @"DHDiagnosisViewController",
                                   nameKey  : @"首页",
                                   imageKey : @"tab_normal_1"
                                   },
                                 @{ClassKey :
                                       @"DHFindViewController",
                                   nameKey  : @"发现",
                                   imageKey : @"tab_normal_2"
                                   },
                                 @{ClassKey :
                                       @"DHBodybuildingViewController",
                                   nameKey  : @"健康",
                                   imageKey : @"tab_normal_3"
                                   }];
    
    /*---------- block ----------*/
    [childItemsArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        
        //快速创建Nav
        DHNavViewController *vc = [self getControllerWithClassName:dict[ClassKey] Title:dict[nameKey] imgName:dict[imageKey]];
        [self addChildViewController:vc];
        
        //自定义选中bar的颜色
        self.tabBar.tintColor = [UIColor redColor];
    }];
}

/**
 快速创建Nav方法
 */
- (DHNavViewController *)getControllerWithClassName:(NSString *)clsName Title:(NSString *)title imgName:(NSString *)imgName {
    
    //根据字符串生成类
    Class cls = NSClassFromString(clsName);
    //创建传入的类的对象
    UIViewController *vc = [[cls alloc] init];
    //记录属性
    self.vc = vc;
    
    //设置文字
    vc.title = title;
    //设置图片
    vc.tabBarItem.image = [UIImage imageNamed:imgName];
    //设置选中状态的图片
    UIImage *selectImg = [UIImage imageNamed:[imgName stringByAppendingString:@"_selected"]];
    
    //设置左导航
    UIButton *LeftItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [LeftItem setBackgroundImage:[UIImage imageNamed:@"menusnew"] forState:UIControlStateNormal];
    self.LeftItem = LeftItem;
    
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:LeftItem];
    vc.navigationItem.leftBarButtonItem = leftButtonItem;
    [LeftItem addTarget:self action:@selector(appearAccountController) forControlEvents:UIControlEventTouchUpInside];
    
    //设置渲染模式
    vc.tabBarItem.selectedImage = [selectImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    DHNavViewController *nav = [[DHNavViewController alloc] initWithRootViewController:vc];
    
    return nav;
}

- (void)appearAccountController {
    //判断按钮状态来实现侧滑
    if (self.LeftItem.selected) {
        [self.vc.mm_drawerController closeDrawerAnimated:YES completion:nil];
        self.LeftItem.selected = NO;
    } else {
        [self.vc.mm_drawerController openDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
        self.LeftItem.selected = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
